package com.fei.controller;

import com.fei.bean.MusicBean;
import com.fei.bean.SingerBean;
import com.fei.mapper.VideoMapper;
import com.fei.service.FileService;
import com.fei.service.MusicService;
import com.fei.service.SingerService;
import com.fei.service.VideoService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
public class SingerController {

    @Autowired
    FileService fileService;
    @Autowired
    SingerService singerService;
    @Autowired
    MusicService musicService;
    @Autowired
    VideoService videoService;


    //歌手管理页面
    @RequestMapping("/admin/toSinger")
    public String toSinger(Model model){
        List<SingerBean> singer = singerService.findAllSingerService();
        model.addAttribute("singerList",singer);
        return "singer";
    }

    @RequestMapping("/ValidSingName")
    @ResponseBody
    public Boolean ValidSingName(String name){
        return singerService.ValidSingName(name);
    }


    //添加歌手
    @RequestMapping("/admin/insertSinger")
    public String insertSinger(SingerBean singerBean, MultipartFile file1,MultipartFile file2){
        //文件上传
        File path = fileService.ParentPath();
        String file1Suffix = fileService.FileSuffix(file1.getOriginalFilename());
        String file2Suffix = fileService.FileSuffix(file2.getOriginalFilename());

        String filePath = path + "//data//images//singer//"+singerBean.getName();
        Boolean f1 = fileService.FileUpload(filePath,"1."+file1Suffix, file1);
        Boolean f2 = fileService.FileUpload(filePath,"2."+file2Suffix,file2);

        singerBean.setHead_img("/images/singer/"+singerBean.getName()+"/1."+file1Suffix);
        singerBean.setIndex_img("/images/singer/"+singerBean.getName()+"/2."+file2Suffix);
        singerService.insertSingerService(singerBean);

        return "redirect:/admin/toSinger";
    }



    //修改歌手信息页面
    @RequestMapping("/admin/update/toSingerUpdate")
    public String toSingerUpdate(int id,Model model){
        SingerBean singer = singerService.selectOneIdService(id);
        model.addAttribute("singer",singer);
        return "singerUpdate";
    }

    //修改歌手信息
    @SneakyThrows
    @RequestMapping("/singerUpdate")
    public String singerUpdate(SingerBean singerBean, MultipartFile file1,MultipartFile file2){

        //获得原有歌手信息
        SingerBean singer = singerService.selectOneIdService(singerBean.getId());
        //项目上级路径
        File path = fileService.ParentPath();
        if(file1.getBytes().length != 0){
            //删除原来的文件
            File file1Delete = new File(path+"//data"+singer.getHead_img());
            file1Delete.delete();
            //上传
            String file1Suffix = fileService.FileSuffix(file1.getOriginalFilename());
            String file1Path = path + "//data//images//singer//"+singerBean.getName();
            fileService.FileUpload(file1Path,"1."+file1Suffix, file1);
            singerBean.setHead_img("/images/singer/"+singerBean.getName()+"/1."+file1Suffix);
        }

        if(file2.getBytes().length != 0){
            //删除原来的文件
            File file1Delete = new File(path+"//data"+singer.getIndex_img());
            file1Delete.delete();

            String file2Suffix = fileService.FileSuffix(file2.getOriginalFilename());
            String file2Path = path + "//data//images//singer//"+singerBean.getName();
            Boolean f2 = fileService.FileUpload(file2Path,"2."+file2Suffix,file2);
            singerBean.setIndex_img("/images/singer/"+singerBean.getName()+"/2."+file2Suffix);
           }

        singerService.updateSingerService(singerBean);
        return "redirect:/admin/toSinger";
    }


    //删除
    @RequestMapping("/admin/delete/singerDelete")
    public String singerDelete(int id){
        File path = fileService.ParentPath();
        SingerBean singerBean = singerService.selectOneIdService(id);

        //删除对应的歌曲作品和对应的数据库信息，以及对应的用户歌单还有歌单中的歌曲数据信息
        musicService.deleteSingerAllService(id);
        String musicPath = path + "//data//Music//"+singerBean.getName();
        fileService.FileDelete(new File(musicPath));

        //删除对应的视频作品,以及对应的数据库信息和视频对应的评论
        videoService.deleteSingerVideoService(id);
        String videoPath = path + "//data//Music//Mv//"+singerBean.getName();
        fileService.FileDelete(new File(videoPath));

        //删除歌手信息
        singerService.deleteSingerService(id);
        String singerPath = path + "//data//images//singer//"+singerBean.getName();
        fileService.FileDelete(new File(singerPath));
        return "redirect:/admin/toSinger";
    }



}
